package com.cty.cookie;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.http.HttpResponse;

@Controller
@RequestMapping("cookie")
@ResponseBody
public class CookieController {

    //获取cookie
    /*
    *
    * 必须在形参列表前加@CookieValue
    *   value = cookie的名字
    *
    * */
    @RequestMapping("data")
    public String data(@CookieValue(value = "cookie") String cookie) {
        System.out.println("cookie = " + cookie);
        return "cookie = " + cookie;
    }

    //保存一个cookie对象
    @RequestMapping("save")
    public String save(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookie","root");
        response.addCookie(cookie);
        return "ok";
    }
}
